package com.resolution.dietaryassessment.view.chronic;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.resolution.dietaryassessment.BaseActivity;
import com.resolution.dietaryassessment.R;
import com.resolution.dietaryassessment.adapter.MyFragmentPagerAdapter;
import com.resolution.dietaryassessment.listener.MyListener;
import com.resolution.dietaryassessment.utils.FragmentNotify;
import com.resolution.dietaryassessment.view.chronic.data.DataUtil;
import com.resolution.dietaryassessment.view.chronic.fragment.ChronicOneFrag;
import com.resolution.dietaryassessment.view.chronic.helper.MyViewPagerHelper;
import com.resolution.dietaryassessment.view.chronic.model.ProgressPointAdapter;
import com.resolution.dietaryassessment.view.chronic.model.SubjectInfo;

import java.util.ArrayList;
import java.util.List;

public class DiabetesSubjectActivity extends BaseActivity implements FragmentNotify {

    private List<Fragment> mFragments = new ArrayList<>();
    private ViewPager2 mViewPager2;

    private RecyclerView mRwList;
    private ProgressPointAdapter Adapter;
    private List<SubjectInfo> data;
    //当前页码
    private int currentPageIndex = 0;

    public static final int QUESTION_COUNT = 25;
    public int FRACTION;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_diabetes_subject);
        initData();
        initView();
        addFragSubjectData();
    }

    /**
     * 数据更新，刷新view
     * @param status 回答对错与否
     */
    @Override
    public void FragNotifyCation(int status) {
        data.get(currentPageIndex).setType(status);
        if (status == MyListener.TRUE_CHOICE){
            FRACTION = FRACTION + 4;
        }
        if (currentPageIndex < data.size()- 1 && status != MyListener.NO_CHOICE){
            currentPageIndex++;
            data.get(currentPageIndex).setState(SubjectInfo.CIRCLE_STATE_CIR);
            data.get(currentPageIndex).setType(MyListener.NO_CHOICE);
            MyViewPagerHelper.setCurrentItem(mViewPager2,currentPageIndex,2000);
        }else if (currentPageIndex == data.size() - 1){
            Intent intent = new Intent(DiabetesSubjectActivity.this,ResultActivity.class);
            intent.putExtra("fraction",Integer.toString(FRACTION));
            startActivity(intent);
        }
        Adapter.notifyDataSetChanged();
    }

    public void initView() {
        //init
        Button mDietary,mTcm,mLeave;
        mDietary = findViewById(R.id.my_dietary);
        mTcm = findViewById(R.id.tcm_btn);
        mLeave = findViewById(R.id.leave_btn);

        mDietary.setOnClickListener(new MyListener(this));
        mTcm.setOnClickListener(new MyListener(this));
        mLeave.setOnClickListener(new MyListener(this));

        //initViewPager
        mViewPager2 = findViewById(R.id.pager_chronic_subject);
        MyFragmentPagerAdapter mAdapter = new MyFragmentPagerAdapter(this, mFragments);
        mViewPager2.setAdapter(mAdapter);

        //initRecyclerView
        mRwList = findViewById(R.id.my_recycler_view);
        Adapter = new ProgressPointAdapter(data, this, R.layout.list_item);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        mRwList.setLayoutManager(linearLayoutManager);
        mRwList.setAdapter(Adapter);
    }

    private void initData() {
        data = new ArrayList<>();
        data.add(new SubjectInfo(SubjectInfo.CIRCLE_STATE_CIR,MyListener.NO_CHOICE));
        for (int i = 0;i < QUESTION_COUNT - 1; i++){
            data.add(new SubjectInfo(SubjectInfo.CIRCLE_STATE_NONE));
        }
    }

    private void addFragSubjectData(){
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getOneData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getTwoData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getThreeData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getFourData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getFiveData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getSixData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getSevenData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getEightData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getNineData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getTenData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getElevenData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getTwelveData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getThirteenData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getFourteenData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getFifteenData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getSixteenData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getSeventeenData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getEighteenData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getNineteenData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getTwentyData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getTwentyOneData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getTwentyTwoData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getTwentyThreeData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getTwentyFourData()));
        mFragments.add(ChronicOneFrag.getInstance(DataUtil.getTwentyFiveData()));
    }
}
